var express = require('express')
var Router = express.Router()
var bodyParser = require('body-parser')
var {date_format,getIp} = require('../utils/utils')
var upload = require('../config/multer')
var dbserver = require('../dao/dbserver')
Router.use(bodyParser.urlencoded({ extended: true }))
Router.use(bodyParser.json())
const port = 3001

//multer有single()中的名称必须是表单上传字段的name名称。
Router.post('/postFile', upload.single('file'), (req, res) => {
    res.send({
        code: 200,
        url: `http://${getIp()}:${port}/uploads/${req.file.filename}`
    })
})
Router.post('/edit/save', (req, res) => {
    let {title, content, edit_img, text } = req.body
    dbserver.create_diary(title,content,edit_img,text,res)
})

Router.get('/edit/get', async(req, res) => {
    dbserver.get_diary_list(res)
})
Router.post('/edit/delete', async(req, res) => {
    let {diary_id} = req.body
    dbserver.delete_diary(diary_id,res)
})

Router.get('/edit/diary/detail',(req,res)=>{
    let {diary_id} = req.query
    dbserver.get_diary_detail(diary_id,res)
})

module.exports = Router